
// g++ -o main main.cpp -Iqtwi/include -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore -I/usr/include/qt4 -Lqtwi/lib -lqtwi -lQtGui -lQtCore -lpthread -lclasslib -ltnW -lnano-X -lmwengine -lmwdrivers -lmxml -lmwfonts -lfreetype

#include <QtWi>
#include <QtWiAux>

#include <iostream>
#include <unistd.h>

// todo: rename engine->core

IProgressBar *pbar;
ILabel *slabel;
IScrollbar* sbar;

volatile int var = 0;
IMutexLock<int> var_lock(var);

using namespace std;

int main(int argc, char **argv) {

	Ifactory *factory; 

#ifdef QTWI_QT
	factory = new QIfactory;
#else
	factory = new TIfactory;
#endif

	// todo: implementations in cpp files
	// todo: destructors!

	IApplication *app = factory->makeApplication(argc, argv);

	try {
		// todo: exceptionsp f9r instance if set App is not done
		factory->setApp(app);

		// todo: put c includes into separate namespace

		// todo: free these here
		IWindow *main = factory->mainWindow("Testwindow", 10, 10);	
		main->resize(580, 500);

		IRawWidget *window = factory->makeRawWidget(main, "", 0, 0);
		window->resize(580, 500);

		//app->setMainWidget(window);
		
		IButton *button = factory->makeButton(window, "Testbutton", 55, 100);
		ILabel *label = factory->makeLabel(window, "LabelTest", 100, 30);
		ICheckButton *checkbutton = factory->makeCheckButton(window, "TestCheckBtn", 200, 50);
		IRadioGroup *radiogroup = factory->makeRadioGroup(window, "MyRadioGroup", 200, 120);
		IRadioButton *radiobutton = factory->makeRadioButton(radiogroup, "First choice", 5, 15);
		IRadioButton *radiobutton2 = factory->makeRadioButton(radiogroup, "Second choice", 5, 40);
		ITextBox* textfield = factory->makeTextBox(window, "", 100, 250, ITextBox::Single);
		ITextBox* textmulti = factory->makeTextBox(window, "", 200, 300, ITextBox::Multi);

		
		sbar = factory->makeScrollbar(window, "", 466, 10, 0, 123, 1, 5, IScrollbar::Vertical);
		pbar = factory->makeProgressBar(window, "", 30, 400, 0, 400, 2);
		slabel = factory->makeLabel(window, "0", 500, 90);
	
		button->setFgColor(IColor::Green);
		/* window->setBgColor(IColor::Blue);
		   button->setBgColor(IColor::Green); */
		button->move(10, 275);	

		textmulti->resize(200, 60);
		
		textfield->show();
		textmulti->show();

		sbar->resize(23, 200);
		sbar->show();
		checkbutton->show();
		label->show();
		button->show();

		main->show();
		window->show();

		radiogroup->resize(200, 100);
		
		button->resize(100, 100);

		pbar->resize(450, 15);					      
		pbar->show();
		slabel->resize(20, 20);
		
		radiogroup->show();
		radiobutton->show();
		radiobutton2->show();

		button->refresh();

		//window->move(800, 400);	
		
		// Action handling
	
		class ProgressActionHandler : public IActionHandler {
		public:
			virtual void handler(IWidget &sender) {
				std::cout << "Action handled: " << &sender << std::endl;
				for(int i = 0; i <= 320; i+=10) {
					usleep(50000);
					pbar->setCurrent(i);
					pbar->refresh();
				}
			}
		};
		
		ProgressActionHandler pah;
		app->registerAction(*button, IAction::CLICK, &pah);

		class SliderActionHandler : public IActionHandler {
		public:
			void handler(IWidget &sender) {
				std::string txt = _qtwi_to_string(sbar->current());
				slabel->setText(txt);
			}
		};

		SliderActionHandler sah;
		app->registerAction(*sbar, IAction::MOD, &sah);

		// Threads

		class MyThread1 : public IThread
		{
		public:
			void action(void *)
			{
				//while(isRunning()) {
				//	var_lock.set(var_lock.read() + 1);
				//}
				while(isRunning())
					var++;
			}
		};

		class MyThread2 : public IThread
		{
		public:
			void action(void *)
			{
				while(isRunning()) {
					cout << "i = " << var_lock.read() << endl;
				}
			}
		};

		MyThread1 th1; // 2x th1 then
		MyThread2 th2;

		th1.start();
		th2.start();

		//th1.join();
		
	
	} catch(IException e) {

		std::cerr << e.info() << std::endl;
	};

	
	return app->exec();
}
